package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Area;
import com.hopu.service.AreaService;
import com.hopu.util.ResultUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("area")
@RestController
public class AraeController {

    @Resource
    private AreaService araeServiceImpl;

    /**
     * 查询区域 分页
     * @param pn
     * @return
     */
    @PostMapping("findAllAreaPageData")
    private ResultUtils findAllAreaPageData(@RequestParam(value = "pn",defaultValue = "1")Integer pn){
        PageHelper.startPage(pn,14);
        List<Area> list = araeServiceImpl.findAllAreaPageData();
        PageInfo page = new PageInfo(list,5);
        return  new ResultUtils(200,"SUCCESS",page);
    }

    /**
     * 添加片区
     * @param aname
     * @return
     */
    @PostMapping("addArea")
    private ResultUtils addArea(@RequestParam(value = "aname",required = true)String aname){
        int num = araeServiceImpl.addArea(aname);
        System.out.println("num="+num);
        if (num>0){
            return  new ResultUtils(200,"SUCCESS",null);
        }
        return  new ResultUtils(400,"ERROR",null);
    }

    /**
     * 根据id 或者name 查询片区
     * @param aname
     * @return
     */
    @PostMapping("getAreaByName")
    private ResultUtils getArea(@RequestParam(value = "aname",required = true)String aname){
            Area area = araeServiceImpl.getAreaByName(aname);
            if (!ObjectUtils.isEmpty(area)) {
                return new ResultUtils(200, "SUCCESS", area);
            }else {
                return  new ResultUtils(400, "SUCCESS", area);
            }
    }

    @PostMapping("getAreaById")
    private ResultUtils getAreaById(@RequestParam(value = "aid",required = true)String aid) {
        Area area = araeServiceImpl.getAreaById(aid);
        if (!ObjectUtils.isEmpty(area)) {
            return new ResultUtils(200, "SUCCESS", area);
        }else {
            return new ResultUtils(400, "ERROR", null);
        }
    }

    /**
     * 修改片区
     * @param aname
     * @param aid
     * @return
     */
    @PostMapping("uppArea")
    private ResultUtils uppArea(@RequestParam(value = "aname",required = true)String aname,
                                @RequestParam(value = "aid",required = true)int aid) {
        System.out.println("aname="+aname);
        System.out.println("aid="+aid);
        int num = araeServiceImpl.uppArea(aname, aid);
        return new ResultUtils(200, "SUCCESS", 1);
    }
    @PostMapping("findAllArea")
    private ResultUtils findAllArea(){
        List<Area> list = araeServiceImpl.findAllArea();
        return  new ResultUtils(200,"SUCCESS",list);
    }
}
